package com.elitel.frame.base.service.impl;

import com.elitel.frame.base.component.exportprojectdata.DataBaseConnInfo;
import com.elitel.frame.base.service.BasicServerService;
import com.elitel.frame.main.dao.BaseServicesMapper;
import com.elitel.frame.main.dao.ext.ExtBaseServicesMapper;
import com.elitel.frame.main.entity.BaseServices;
import com.elitel.frame.main.entity.DataSourceType;
import com.elitel.frame.main.entity.vo.PageResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BasicServerServiceImpl implements BasicServerService {

    @Autowired
    private BaseServicesMapper baseServicesMapper;
    @Autowired
    private ExtBaseServicesMapper extBaseServicesMapper;
    @Autowired
    private DataBaseConnInfo dataBaseConnInfo;

    @Override
    public String insertBasicServer(BaseServices basicserver) {
        String message = "fail";
        Long id = System.currentTimeMillis();
        basicserver.setGuid(String.valueOf(id));
        int a = baseServicesMapper.insertSelective(basicserver);
        if (a > 0) {
            message = "success";
        }
        return message;
    }

    @Override
    public String updateServer(BaseServices basicserver) {
        String message = "fail";
        int a = baseServicesMapper.updateByPrimaryKeySelective(basicserver);
        if (a > 0) {
            message = "success";
        }
        return message;
    }

    @Override
    public PageResponseVo<BaseServices> selectBasicFenYe(
            String pageNo, String pageSize, String searchContent) {
        String dbType = dataBaseConnInfo.dataBaseType;
        int lineEnd = Integer.parseInt(pageNo) * Integer.parseInt(pageSize);
        int lineStart = lineEnd - Integer.parseInt(pageSize) + 1;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("lineEnd", lineEnd);
        map.put("lineStart", lineStart - 1);
        map.put("searchContent", searchContent);
        List<BaseServices> list = null;
        if ("mysql".equals(dbType) || "sqlite".equals(dbType)) {
            list = extBaseServicesMapper.selectBasicByPageMySql(map);
        } else if ("oracle".equals(dbType)) {
            list = extBaseServicesMapper.selectBasicByPage(map);
        } else if ("h2".equals(dbType)) {
            list = extBaseServicesMapper.selectBasicByPageMySql(map);
        } else if (DataSourceType.POSTGRES.equals(dbType)) {
            list = extBaseServicesMapper.selectBasicByPagePG(map);
        }
        // List<BaseServices> list = extBaseServicesMapper.selectBasicByPage(map);
        int count = extBaseServicesMapper.selectCount(searchContent);
        PageResponseVo<BaseServices> pageResponseVo = new PageResponseVo<BaseServices>();
        pageResponseVo.setRows(list);
        pageResponseVo.setTotal(count);
        return pageResponseVo;
    }

    @Override
    public BaseServices selectServerOne(String id) {
        BaseServices baseServices = baseServicesMapper.selectByPrimaryKey(id);
        return baseServices;
    }

    @Override
    public String deleteServer(String id) {
        String message = "fail";
        int a = baseServicesMapper.deleteByPrimaryKey(id);
        if (a > 0) {
            message = "success";
        }
        return message;
    }

    @Override
    public PageResponseVo<BaseServices> selectBasicServerAll() {
        PageResponseVo<BaseServices> pageResponseVo = new PageResponseVo<BaseServices>();
        List<BaseServices> list = baseServicesMapper.selectBasicServerAll();
        pageResponseVo.setRows(list);
        pageResponseVo.setTotal(list.size());
        return pageResponseVo;
    }

    @Override
    public int selectExit(String guid) {
        int count = extBaseServicesMapper.selectExit(guid);
        return count;
    }
}
